const std = @import("std");

pub fn build(b: *std.Build) void {
    const target = b.standardTargetOptions(.{});
    const optimize = b.standardOptimizeOption(.{});

    // Kernel syscall library
    const syscall_lib = b.addStaticLibrary(.{
        .name = "asterinas-syscall-zig",
        .root_source_file = b.path("src/lib.zig"),
        .target = target,
        .optimize = optimize,
    });

    // Configure for bare metal kernel environment
    syscall_lib.setLibCFile(b.path("libc.conf"));
    syscall_lib.bundle_compiler_rt = true;
    syscall_lib.red_zone = false;
    syscall_lib.omit_frame_pointer = false;
    syscall_lib.stack_protector = false;

    // Add C bindings for Rust integration
    syscall_lib.addCSourceFile(.{
        .file = b.path("src/c_bindings.c"),
        .flags = &.{
            "-std=c11",
            "-fno-stack-protector",
            "-mno-red-zone",
            "-mcmodel=kernel",
            "-fPIC",
        },
    });

    b.installArtifact(syscall_lib);

    // Tests
    const tests = b.addTest(.{
        .root_source_file = b.path("src/lib.zig"),
        .target = target,
        .optimize = optimize,
    });

    const test_step = b.step("test", "Run syscall tests");
    test_step.dependOn(&b.addRunArtifact(tests).step);

    // Examples
    const example = b.addExecutable(.{
        .name = "syscall_example",
        .root_source_file = b.path("examples/basic_usage.zig"),
        .target = target,
        .optimize = optimize,
    });
    
    example.linkLibrary(syscall_lib);
    
    const example_step = b.step("example", "Build syscall example");
    example_step.dependOn(&b.addInstallArtifact(example, .{}).step);
}